<template>
  <!-- 遮罩层 -->
  <view v-if="show" class="modal-mask" @click="handleMaskClick">
    <!-- 弹窗容器 -->
    <view class="modal-container" :style="containerStyle" @click.stop>
      <!-- 背景图片 -->
      <image
          v-if="backgroundImage"
          :src="backgroundImage"
          class="modal-bg"
          mode="widthFix"
      ></image>

      <!-- 关闭按钮 -->
      <view class="close-btn" @click="handleClose">
        <u-icon name="close" color="#666666" size="20"></u-icon>
      </view>

      <!-- 内容区域 -->
      <view class="modal-content">
        <!-- 标题 -->
        <view v-if="title" class="modal-title">
          {{ title }}
        </view>

        <!-- 插槽内容 -->
        <view class="content">
          <slot></slot>
        </view>
        <!-- 按钮组 -->
        <view class="button-group">
          <view
              v-if="buttons.showCancel"
              class="btn cancel-btn"
              @click="handleCancel"
          >
            {{ buttons.cancelText || '取消' }}
          </view>
          <view
              v-if="buttons.showConfirm"
              class="btn confirm-btn"
              @click="handleConfirm"
          >
            {{ buttons.confirmText || '确定' }}
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'custom-modal',
  props: {
    // 是否显示模态框
    show: {
      type: Boolean,
      default: false
    },
    // 标题
    title: {
      type: String,
      default: '温馨提示'
    },
    // 背景图片URL（固定路径）
    backgroundImage: {
      type: String,
      default: '/static/images/model-bg.png'  // 固定背景图片路径
    },
    // 按钮配置
    buttons: {
      type: Object,
      default: () => ({
        showCancel: true,
        showConfirm: true,
        confirmText: '确定',
        cancelText: '取消',
        confirmColor: '#FC4837',
        cancelColor: '#999999'
      })
    },
    // 弹窗配置
    options: {
      type: Object,
      default: () => ({
        width: '650rpx',
        zIndex: 10075,
        maskCloseAble: true,
        negativeTop: 0,
        closeOnClickOverlay: true,
        customStyle: {}
      })
    }
  },
  computed: {
    containerStyle() {
      return {
        width: this.options.width || '650rpx',
        ...(this.options.customStyle || {})
      }
    }
  },
  methods: {
    handleMaskClick() {
      if (this.options.closeOnClickOverlay) {
        this.handleClose()
      }
    },
    handleClose() {
      this.$emit('update:show', false)
      this.$emit('close')
    },
    handleCancel() {
      this.$emit('cancel')
      this.handleClose()
    },
    handleConfirm() {
      this.$emit('confirm')
      this.handleClose()
    }
  }
}
</script>
<style lang="scss" scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;

  .modal-container {
    position: relative;
    width: 650rpx;
    min-height: 400rpx; // 最小高度
    border-radius: 24rpx;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding-bottom: 20rpx;
    z-index: 1001;

    .modal-bg {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: auto;
      min-height: 100%;
      min-width: 100%;
      z-index: 0;
    }

    .close-btn {
      position: absolute;
      right: 20rpx;
      top: 73rpx;
      padding: 10rpx;
      z-index: 10;
    }

    .modal-content {
      position: relative;
      z-index: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .content {
        min-height: 78rpx;
        padding: 0rpx 24rpx;
        display: flex;
        justify-content: center;
      }

      .modal-title {
        font-weight: bold;
        font-size: 36rpx;
        color: #FC4837;
        margin: 60rpx 0 36rpx 0;
        width: 100%;
        text-align: center;
      }

      .button-group {
        display: flex;
        justify-content: center;
        width: 100%;
        margin: 0 0 39rpx 0;
        line-height: 100%;

        .btn {
          border-radius: 10rpx;
          margin: 0 10rpx;

          &::after {
            border: none;
          }

          &.cancel-btn {
            font-family: PingFang SC;
            font-weight: 400;
            font-size: 26rpx;
            color: #FF4400;
            border: 1rpx solid #FF4400;
            padding: 21rpx 53rpx;
          }

          &.confirm-btn {
            background: #FF4400;
            border-radius: 10rpx;
            font-family: PingFang SC;
            font-weight: 400;
            font-size: 26rpx;
            color: #FFFFFF;
            padding: 21rpx 53rpx;
          }
        }
      }
    }
  }
}
</style>


